package com.minjiang.blog.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.minjiang.blog.mode.Tag;
import com.minjiang.blog.mode.Type;
import com.minjiang.blog.service.TagService;
import com.minjiang.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class TagController {
    @Autowired
    private TagService tagService;

    /**
     * 分页查询标签列表
     *
     * @param model
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/tags", method = RequestMethod.GET)
    public String types(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        String orderBy = "id desc";
        List<Tag> allTag = tagService.findAllTag();
        //初始化设置
        PageHelper.startPage(pageNum, 10, orderBy);
        //放入数据
        PageInfo<Tag> pageInfo = new PageInfo<>(allTag);
        //显示数据
        model.addAttribute("pageInfo", pageInfo);

        System.out.println(allTag);
        return "admin/tags";
    }
    //----------------------------------------------------------------------------------------

    /**
     * 返回新增标签页面
     *
     * @return
     */
    @RequestMapping(value = "/tags/input", method = RequestMethod.GET)
    public String TagsInput(Model model) {
        model.addAttribute("tag", new Tag());
        return "admin/tags-add";
    }

    /**
     * 新增标签
     *
     * @param tag
     * @param redirectAttributes
     * @return
     * @Valid 用于实体数据的检验，错误自动获取message
     */
    @RequestMapping(value = "/tags/add", method = RequestMethod.POST)
    public String typeAdd(@Valid Tag tag, RedirectAttributes redirectAttributes) {
        Tag tagByName = tagService.getTagByName(tag.getName());
        if (tagByName != null) {
            redirectAttributes.addFlashAttribute("message", "不能添加重复的分类");
            return "redirect:/admin/tags/input";
        }
        int saveType = tagService.saveTage(tag);
        if (saveType > 0) {
            redirectAttributes.addFlashAttribute("message", "添加成功");
        } else {
            redirectAttributes.addFlashAttribute("message", "添加失败");
        }
        return "redirect:/admin/tags";
    }

    //----------------------------------------------

    /**
     * 跳转修改标签页面
     *
     * @param id
     * @param model
     * @return
     * @Valid 用于实体数据的检验，错误自动获取message
     */
    @RequestMapping(value = "/tags/update/{id}", method = RequestMethod.GET)
    public String typeRedirects(@PathVariable Long id, Model model) {
        //将id搜索到的值赋予mode，用于页面的呈现
        model.addAttribute("mode", tagService.findTagById(id));
        return "admin/tags-input";
    }

    /**
     * 编辑修改标签页面
     *
     * @param
     * @param tag
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/tags/edit/{id}", method = RequestMethod.POST)
    public String typeEdit(@Valid Tag tag, RedirectAttributes redirectAttributes) {
        int saveTage = tagService.updateTag(tag);
        if (saveTage > 0) {
            redirectAttributes.addFlashAttribute("message", "编辑成功");
        } else {
            redirectAttributes.addFlashAttribute("message", "编辑失败");
        }
        return "redirect:/admin/tags";
    }

    /**
     * 删除分类
     *
     * @param id
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/tags/delete/{id}", method = RequestMethod.GET)
    public String typeDelete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
        tagService.deleteTag(id);
        redirectAttributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/tags";

    }
}
